package com.bdy.lm.taximanager.thread;

import android.content.Context;
import android.content.SharedPreferences;

import com.bdy.lm.taximanager.app.MyApp;
import com.bdy.lm.taximanager.http.HttpJson;
import com.bdy.lm.taximanager.model.Fare;
import com.bdy.lm.taximanager.model.MessagePackaging;
import com.bdy.lm.taximanager.port.TaxiMeterDataAnalysis;
import com.bdy.lm.taximanager.util.JsonUtils;

import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import de.greenrobot.event.EventBus;

/**
 * Created by Selfmade on 2017/8/7/0007.
 */

public class ModifyFareThread extends Thread {

    private SharedPreferences sharedPreferences;
    private MessagePackaging messagePackaging;
    private HttpJson httpJson;
    private JSONObject jsonObject;
    private Logger logger;
    private String fareJsonString;

    public ModifyFareThread() {
        logger = Logger.getLogger(this.getClass());
        httpJson = new HttpJson();
        messagePackaging = new MessagePackaging(112);
        jsonObject = JsonUtils.mapToJsonObject(messagePackaging.getFareInfoMap());
    }

    @Override
    public void run() {
        sharedPreferences = MyApp.context.getSharedPreferences("FareInfo", Context.MODE_PRIVATE);
        if (sharedPreferences.contains("fare")) {//若本地有存储的运价信息
            MyApp.fare = JsonUtils.gson.fromJson(sharedPreferences.getString("fare", null), Fare.class);
            logger.info("本地已有Fare:" + MyApp.fare);
        }
        while(true) {
            //首先从服务器获取最新的运价信息
            if(httpJson.getPostJson() == null) {
                httpJson.HttpJsonPost(jsonObject, MyApp.serverUrl);
            } else {
                logger.info("FareJsonObject:" + httpJson.getPostJson().toString());
                try{
                    fareJsonString = httpJson.getPostJson().getJSONObject("fare").toString();
                    Fare f = JsonUtils.gson.fromJson(fareJsonString, Fare.class);
                    logger.info("FareModel:" + f);
                    if (MyApp.fare != null && !f.equals(MyApp.fare)) {//对比与本地存储的运价信息是否相同,若不同则更新本地运价信息并修改计价器的运价信息
                        sharedPreferences.edit().putString("fare", fareJsonString).commit();
                        TaxiMeterDataAnalysis.newInstance().modifyFare(MyApp.fare);
                        EventBus.getDefault().post(MyApp.fare);//通知主线程更新界面 昼夜间
                    } else {//若本地未存储运价相关信息，则先存到本地再修改计价器的运价信息
                        sharedPreferences.edit().putString("fare", fareJsonString).commit();
                        MyApp.fare = f;
                        TaxiMeterDataAnalysis.newInstance().modifyFare(MyApp.fare);
                        EventBus.getDefault().post(MyApp.fare);
                    }
                }catch (JSONException e) {
                    e.printStackTrace();
                    logger.error("Fare获取运价信息失败" + e);
                }
                break;
            }
            delay(10000);
        }
    }
    /**
     * 延时
     * @param time 毫秒
     */
    private void delay(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            logger.error(e);
            e.printStackTrace();
        }
    }
}
